{$R-}

(*
	GetDocs -- update document representatives stack

	pascal GetDocs.p
	link -m ENTRYPOINT -o {BOOT}docs -rt XCMD=2 -sn Main=GetDocs GetDocs.p.o ¶
	  {MPW}Libraries:Interface.o {MPW}PLibraries:PasLib.o
        {boot}hypercard
	
*)

{$S GetDocs } 	{ Segment name must be the same as the command name. }

UNIT DummyUnit;

INTERFACE

USES MemTypes, QuickDraw, OSIntf, ToolIntf, HyperXCmd;

PROCEDURE EntryPoint(paramPtr: XCmdPtr);
	
IMPLEMENTATION

TYPE Str31 = String[31];

PROCEDURE GetDocs(paramPtr: XCmdPtr); 							FORWARD;

  PROCEDURE EntryPoint(paramPtr: XCmdPtr);
  BEGIN
    GetDocs(paramPtr);
  END;

  PROCEDURE GetDocs(paramPtr: XCmdPtr);
  VAR pathName: Str255;
      fileName:	Str255;
      paramBlock: CInfoPBRec;
      deskTop: INTEGER;
	  
    {$I XCmdGlue.inc }
    
    FUNCTION CardExists: BOOLEAN;
    { do we already have a card for this document? }
    VAR result: Handle;
    BEGIN
      SendCardMessage(Concat('find "',fileName,'"'));
      result := EvalExpr('the result');
      CardExists := result^^ = 0;
      DisposHandle(result);
    END;
    
    PROCEDURE PutField(fieldName,fieldVal: Str255);
    VAR h: Handle;
    BEGIN
      h := PasToZero(fieldVal);
      SetFieldByName(FALSE,fieldName,h);
      DisposHandle(h);
    END;
    
    FUNCTION OSTypeToStr(str: OSType): Str31;
    VAR result: Str31;
    BEGIN
      result[0] := CHR(4);
      BlockMove(@str,Pointer(ORD(@result)+1),4);
      OSTypeToStr := result;
    END;
	  
    PROCEDURE DoOneFile;
    TYPE PasPtr = ^Str255;
    VAR cmnt: Handle;
    BEGIN
      IF CardExists THEN EXIT(DoOneFile);
      SendCardMessage('go to last card');
      SendCardMessage('doMenu "New Card"');
      PutField('Name',fileName);
      PutField('Where',pathName);
      PutField('Type',OSTypeToStr(paramBlock.ioFlFndrInfo.fdType));
      PutField('Creator',OSTypeToStr(paramBlock.ioFlFndrInfo.fdCreator));
      PutField('Created',LongToStr(paramBlock.ioFlCrDat));
      SendCardMessage('convert field "Created" to long date');
      PutField('Modified',LongToStr(paramBlock.ioFlMdDat));
      SendCardMessage('convert field "Modified" to long date');
      PutField('Size',Concat(LongToStr((paramBlock.ioFlPyLen+paramBlock.ioFLRPyLen+1023) DIV 1024),' K'));
      cmnt := GetResource('FCMT',paramBlock.ioFlXFndrInfo.fdComment);
      IF cmnt <> NIL THEN PutField('Notes',PasPtr(cmnt^)^);
      (***
      ioFlXFndrInfo.fdIconID
      ***)
    END;
      
    PROCEDURE DoOnePath;
    VAR fileIndex: INTEGER;
	result:	INTEGER;
	wdParams: WDPBRec;
    BEGIN
      { set up working directory }
      ZeroBytes(@wdParams,SizeOf(wdParams));
      WITH wdParams DO
	BEGIN
	  ioNamePtr := @pathName;
	  ioWDProcID := $4552494B;  { 'ERIK' so finder will delete later }
	  ioWDDirID := 2;
	END;
      SetResLoad(FALSE);
      SetResLoad(TRUE);
      result := PBOpenWD(@wdParams,FALSE);
      IF result <> 0 THEN EXIT(DoOnePath);

      { step through each file in this directory }
      fileIndex := 1;
      REPEAT
	ZeroBytes(@paramBlock,SizeOf(paramBlock));
	WITH paramBlock DO
	  BEGIN
	    fileName := '';
	    ioNamePtr := @fileName;
	    ioVRefNum := wdParams.ioVRefNum;
	    ioFDirIndex := fileIndex;
	  END;
	result := PBGetCatInfo(@paramBlock,FALSE);
	IF (result = 0) 
	AND NOT BitTst(@paramBlock.ioFlAttrib,3) 
	AND (paramBlock.ioFlFndrInfo.fdType <> 'APPL') 
	AND (paramBlock.ioFlFndrInfo.fdType <> 'FNDR') 
	THEN DoOneFile;
	fileIndex := fileIndex + 1;
      UNTIL result = fnfErr;
    END;

  BEGIN
    WITH paramPtr^ DO
      BEGIN
        IF paramCount < 1 THEN
	  BEGIN
	    returnValue := PasToZero('search which folder?');
	    EXIT(GetDocs);
	  END;
	ReturnToPas(params[1]^,pathName);
        deskTop := OpenRFPerm('DeskTop',0,fsRdPerm);
	DoOnePath;
	CloseResFile(deskTop);
      END;
  END;

END.



